<?php

class Customers_model extends Model {
	
	
	var $customer_id;
	var $last_name;
	var $first_name;
	var $middle_initial;
	var $dob;
	var $customer_gender_id;
	var $race_id;
	var $home_address;
	var $home_city_id;
	var $state_id;
	var $zip_code;
	var $home_phone;
	var $work_name;
	var $work_phone;
	var $photo_id_number;
	var $photo_id_type;
	var $photo_id_state;
	var $height;
	var $weight;
	var $eye_color_id;
	var $hair_color_id;
	var $customer_note;
	
	function Customers_model()
	{
		parent::Model();
	}
	
	protected function filterInputArray($table, $data, $xss_clean = false){
        $fields = $this->db->list_fields($table);

        foreach ($data as $k => $v) {
                if(in_array($k, $fields) == false){
                        unset($data[$k]);
                } else {
                        if($xss_clean === true) $data[$k] = $this->input->xss_clean($v);
                }
        }

        return $data;
	}

	
	function get() {
		
		// If any of the following properties are set before customers_model->get() is called from the controller
		//  then we will include a where statement for each of the properties that have been set.
        		
		if ($this->customer_id) {
            $this->db->where("customer_id", $this->customer_id);
        }

        if ($this->last_name) {
            $this->db->where("last_name", $this->last_name);
        }

        if ($this->first_name) {
            $this->db->where("first_name", $this->first_name);
        }

        if ($this->home_phone) {
            $this->db->where("home_phone", $this->home_phone);
        }
		
		$this->db->select('customer_id,last_name,first_name,middle_initial,dob,customers.customer_gender_id, customer_genders.customer_gender');
		$this->db->select('customers.race_id,races.race_description,home_address,customers.home_city_id,home_cities.city');
		$this->db->select('customers.state_id,states.state_abbreviation,zip_code,home_phone,work_name,work_phone');
		$this->db->select('photo_id_number,photo_id_type,photo_id_state,height,weight');
		$this->db->select('customers.eye_color_id,eye_colors.eye_color,customers.hair_color_id,hair_colors.hair_color,customer_note');
		$this->db->from('customers');
		$this->db->join('home_cities','customers.home_city_id = home_cities.home_city_id','left');
		$this->db->join('customer_genders','customers.customer_gender_id = customer_genders.customer_gender_id',							'left');
		$this->db->join('races','customers.race_id = races.race_id','left');
		$this->db->join('states','customers.state_id = states.state_id','left');
		$this->db->join('eye_colors','customers.eye_color_id = eye_colors.eye_color_id','left');
		$this->db->join('hair_colors','customers.hair_color_id = hair_colors.hair_color_id','left');
		
		
		// We will display our results in order by last name and then first name.
        $this->db->orderby("last_name, first_name");

		$query = $this->db->get();
		
		// If you set Customer_model->customer_id from your controller, then there will only be one row to return.
        if ($this->customer_id) {
            return ($query->row());		// returns an object
        }
        else
           	return $query->result();	// returns an array of objects
	}
	
	function save($data)
	{
		// Allows you to add a new customer as well as update an existing customer
		$db_array = $this->filterInputArray('customers', $data);


//  		$db_array = array(
//  	        "last_name" => $this->last_name,
//  	        "first_name" => $this->first_name,
//  	        "middle_initial" => $this->middle_initial,
//  	        "dob" => $this->dob,
//  	        "customer_gender_id" => $this->customer_gender_id,
//  	        "race_id" => $this->race_id,
//  	        "home_address" => $this->home_address,
//  	        "home_city_id" => $this->home_city_id,
//  	        "state_id" => $this->state_id,
//  	        "zip_code" => $this->zip_code,
//  	        "home_phone" => $this->home_phone,
//  	        "work_name" => $this->work_name,
//  	        "work_phone" => $this->work_phone,
//  	        "photo_id_number" => $this->photo_id_number,
//  	        "photo_id_type" => $this->photo_id_type,
//  	        "photo_id_state" => $this->photo_id_state,
//  	        "height" => $this->height,
//  	        "weight" => $this->weight,
//  	        "eye_color_id" => $this->eye_color_id,
//  	        "hair_color_id" => $this->hair_color_id,
//  	        "customer_note" => $this->customer_note);
//    
 

        // If Customers_model->customer_id was set in the controller, then we will update an existing record.
        if ($this->customer_id) {
            $this->db->where("customer_id", $this->customer_id);
            $this->db->update("customers", $db_array);
        }

        // If Customers_model->customer_id was not set in the controller, then we will insert a new record.
        else {
            $this->db->insert("customers", $db_array);
        }
    }
	
	function delete($id)
	{
		// function deletes a customer record based on the Id value
		// As long as Customers_model->customer_id was set in the controller, we will delete the record.
        
        $this->customer_id = $id;
        
		if ($this->customer_id) {
            $this->db->where("customer_id", $this->customer_id);
            $this->db->delete("customers");
        }
		
	}
	
	// Lookup Functions
	
	function get_Cities() {
			
		$query = $this->db->get('home_cities');
		if ($query->num_rows() > 0) {
			return $query->result();
		}
		else
			return FALSE;
	}
	function get_Genders() {
			
		$query = $this->db->get('customer_genders');
		if ($query->num_rows() > 0) {
			return $query->result();
		}
		else
			return FALSE;
	}
	
	function get_Races() {
			
		$query = $this->db->get('races');
		if ($query->num_rows() > 0) {
			return $query->result();
		}
		else
			return FALSE;
	}
	
	function get_States() {
			
		$query = $this->db->get('states');
		if ($query->num_rows() > 0) {
			return $query->result();
		}
		else
			return FALSE;
	}
	
	function get_Eye_colors() {
			
		$query = $this->db->get('eye_colors');
		if ($query->num_rows() > 0) {
			return $query->result();
		}
		else
			return FALSE;
	}
	
	function get_Hair_colors() {
			
		$query = $this->db->get('hair_colors');
		if ($query->num_rows() > 0) {
			return $query->result();
		}
		else
			return FALSE;
	}
	
	
	
}




/** lookup code from internet 

  echo '<select name="source" id="source">';
  $result = mysql_query("SELECT * FROM source");
  while($row = mysql_fetch_array($result)){
    echo '<option value="'.$row['source_id'].'"';
    if($client_info['source_id'] == $row['source_id']) echo ' selected';
    echo '>'.$row['source'].'</option>';
  }
  echo '</select>';
 */

?>